package cn.edu.sdjzu.xg.kcsj.security;

import cn.edu.sdjzu.xg.kcsj.exception.BysjException;
import cn.edu.sdjzu.xg.kcsj.util.JdbcHelper;
import cn.edu.sdjzu.xg.kcsj.util.Pagination;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;
import java.util.TreeSet;

public class UserService {
    private UserService(){}
    private static UserDao userDao = UserDao.getInstance();
    private static UserService userService = new UserService();

    public static UserService getInstance(){
        return userService;
    }

    // 查找全部用户
    public Collection<User> findAll(List conditionList, Pagination pagination) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        //将自动提交设为false,开始事务
        connection.setAutoCommit(false);
        Collection<User> users = new TreeSet<>();
        try {
            users = userDao.findAll(conditionList,pagination,connection);
            //提交事务
            connection.commit();
        }catch (SQLException e){
            //回滚
            connection.rollback();
        }finally {
            //将自动提交设为true，结束事务
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return users;
    }

    // 按id查找用户
    public User find(Integer id) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        //将自动提交设为false,开始事务
        connection.setAutoCommit(false);
        User user = null;
        try {
            user = userDao.find(id,connection);
            //提交事务
            connection.commit();
        }catch (SQLException e){
            //回滚
            connection.rollback();
        }finally {
            //将自动提交设为true，结束事务
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return user;
    }

    // 更新一条
    public boolean update(User user) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        //将自动提交设为false,开始事务
        connection.setAutoCommit(false);
        Boolean updated =false;
        try {
            updated = userDao.update(user,connection);
            //提交事务
            connection.commit();
        }catch (SQLException e){
            //回滚
            connection.rollback();
        }finally {
            //将自动提交设为true，结束事务
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return updated;
    }

    // 增加
    public boolean add(User user) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        //将自动提交设为false,开始事务
        connection.setAutoCommit(false);
        Boolean added =false;
        try {
            //增加
            added = userDao.add(user,connection);
            //提交事务
            connection.commit();
        }catch (SQLException e){
            //回滚
            connection.rollback();
        }finally {
            //将自动提交设为true，结束事务
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return added ;
    }

    // 删除
    public boolean delete(Integer id) throws SQLException, BysjException {
        Connection connection = JdbcHelper.getConn();
        //将自动提交设为false,开始事务
        connection.setAutoCommit(false);
        Boolean deleted = false;
        try {
            deleted = userDao.delete(id,connection);
            connection.commit();
        } catch (SQLException e) {
            //回滚
            connection.rollback();
        } finally {
            //将自动提交设为true，结束事务
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return deleted;
    }

    public User login(String username,String password) throws SQLException,BysjException{
        Connection connection = JdbcHelper.getConn();
        //将自动提交设为false,开始事务
        connection.setAutoCommit(false);
        User user = null;
        try {
            user = userDao.login(connection,username,password);
            //提交事务
            connection.commit();
        }catch (SQLException e){
            //回滚
            connection.rollback();
        }finally {
            //将自动提交设为true，结束事务
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return user;
    }
}
